<template>
  <div class="login-container">
    <div class="head-title">
      <img src="../../assets/logo.png" class="head-title-img">
      <span class="head-title-text"
      >{{ headTitle }}
      </span>
    </div>
    <div class="login-card">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
               label-position="left"
      >

        <div class="title-container">
          <h1 class="title">登录</h1>
        </div>

        <el-form-item prop="adminName">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
          <el-input
            ref="adminName"
            v-model="loginForm.adminName"
            placeholder="请输入账号"
            name="adminName"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"/>
        </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
        </el-form-item>

        <el-button
          :loading="loading"
          class="login-form-button"
          @click.native.prevent="handleLogin"
        >登录
        </el-button>
        <el-button
          class="login-form-button"
          @click.native.prevent="handleLogin"
        >注册
        </el-button>
      </el-form>
    </div>
    <div class="foot-title">{{ footTitle }}</div>
  </div>
</template>

<script>
  // import { validUsername } from '@/utils/validate'

  export default {
    name: 'Login',
    data() {
      const validateUsername = (rule, value, callback) => {
        callback()
        if (value.length < 6) {
          callback(new Error('请输入正确的账号'))
        } else {
          callback()
        }
      }
      const validatePassword = (rule, value, callback) => {
        if (value.length < 6) {
          callback(new Error('密码不能少于6位'))
        } else {
          callback()
        }
      }
      return {
        headTitle: '长沙电务段安控体系平台管理系统',
        footTitle: '京ICP备05020493号-4 | ICP证：京B2-20202537',
        loginForm: {
          adminName: '张志斌',
          password: 'ho/hLMN2/ltPI'
        },
        loginRules: {
          adminName: [{ required: true, message: '请输入用户名称', trigger: 'blur', validator: validateUsername }],
          password: [{ required: true, message: '请输入用户密码', trigger: 'blur', validator: validatePassword }]
        },
        loading: false,
        passwordType: 'password',
        redirect: undefined
      }
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect
        },
        immediate: true
      }
    },
    methods: {
      showPwd() {
        if (this.passwordType === 'password') {
          this.passwordType = ''
        } else {
          this.passwordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store.dispatch('user/login', this.loginForm).then(() => {
              this.$router.push({path: this.redirect || '/'})
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */

$bg: #6699CC;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: black;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$dark_gray: #889aa4;
$light_gray: #eee;
$color-shadow: #babecc;
$color-white: #fff;
$img: 'https://images.unsplash.com/photo-1587995263354-5cd478145006?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1332&q=80';

.head-title {
  display: flex;
  margin: 20px;
  padding: 10px;

  .head-title-img {
    width: 50px;
    height: 50px;
    border-radius: 10px;
  }

  .head-title-text {
    font-size: 250%;
    font-family: 黑体, sans-serif;
    padding-left: 10px;
    padding-top: 10px;
    color: #FFFFFF;
    text-align: center;
    //text-shadow: 0 0 0.1em, 0 0 0.1em;
  }
}

.foot-title {
  display: flex;
  justify-content: center;
  margin-top: 160px;
  color: #FFFFFF;
  font-size: 8px;
}

.login-container {
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
  background-image: url($img);
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
  background-position: top;
  overflow: hidden;

  .login-card {
    width: 350px;
    margin: 80px auto;
    border: 1px solid #eaeaea;
    border-radius: 15px;
    box-shadow: 0 0 25px #000000;
    background: hsla(0, 0%, 100%, .7);
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 50px 35px 0;
    margin: 0 auto;
    overflow: hidden;

    .el-form-item {
      background: rgb(89, 88, 88, .5);
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: #000000;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-family: 黑体, sans-serif;
      //font-size: 45px;
      color: #3491fa;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #000000;
    cursor: pointer;
    user-select: none;
  }

  .login-form-button {
    font-family: Sans-serif, Arial, serif;
    border: 0;
    outline: 0;
    font-size: 18px;
    border-radius: 10px;
    padding: 12px;
    color: #52a3ff;
    font-weight: bold;
    width: 40%;
    margin: 20px 13px;
    transition: all 0.2s ease-in-out;
    cursor: pointer;

    &:hover {
      box-shadow: -2px -2px 5px $color-white, 2px 2px 5px $color-shadow;
    }
  }
}
</style>
